/*
 *  HeartBeatController.cpp
 *  project8
 *
 *  Created by Luca Gobbo on 21-06-10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "MonitorController.h"
#include "string.h"   

MonitorController::MonitorController(Button* set,Button* mode,DisplayMonitor* dm, HeartBeatController * hbc):
	cTask("MonitorControlTask",3)
  , buttonFlags(5)
  , theSetButton(set)
  , theModeButton(mode)
  , theDisplayMonitor(dm)
  , theHeartBeatController(hbc) { }

void MonitorController::init(void){
    
    theSetButton->setListener(this);
    theModeButton->setListener(this);
    
    DisplayItem di_up;
    DisplayItem di_down;
    di_up.len = 15;
    di_up.row = 0;
    di_down.len = 15;
    di_down.row = 1;
    
    for (int i = 0; i < 15; i++){
        di_up.buf[i] = ' ';
        di_down.buf[i]=  ' ';
    }
    theDisplayMonitor->display(di_up);
    theDisplayMonitor->display(di_down);
    
    
    state = STB;
    buttonFlags.wait();
}

void MonitorController::timeout(Timer* src) {
	buttonFlags.set(timerFlagNo);
  
  
	
}



void MonitorController::update(void){ 
	
  	switch(buttonFlags.getFlagNo()){
      case  btnFlagNo0:
        //up set button
        break;
      case  btnFlagNo1:
        std::cout << "ja";
        if (state != STB){
          std::cout << "zeker";
          clock.setIncValue(0);
          theHeartBeatController->disable();
        }
        break;
      case  btnFlagNo2:
        if (state == STB){
          theHeartBeatController->run();
          timer.set(100 MS,this);
          state = AVG;
        } else if (state == AVG){
          state = HIGH;  
        } else if (state == HIGH){
          state = LOW;
        } else if (state == LOW){
          state = AVG;          
        }
        break;
      case  btnFlagNo3: 
        //down mode button 
        break;
      case timerFlagNo:
        if (state != STB){ //dus runing maar kan in andere modes bevinden
          timer.set(100 MS,this);
        	clock.tick();
        	char timeString [10];
        	
        	clock.getTimeString(timeString);
        	DisplayItem di;
        	di.len = 15;
        	di.row = 0;
        	my_strcopy(timeString, di.buf);
        	
        	int hb = theHeartBeatController->getHbCur();
        	char buffer[3];
        	int_to_char(hb, buffer);
        	di.buf[10] = ' ';
        	di.buf[11] = ' ';
        	di.buf[12] = buffer[0];
        	di.buf[13] = buffer[1];
        	if (hb > 99)
        	di.buf[14] = buffer[2];
          else
          di.buf[14] = ' ';
        	theDisplayMonitor->display(di);
          
        	
        	DisplayItem onder;
        	onder.len = 15;
        	onder.row = 1;
        	
        	for (int i = 0; i < 15; i++)
        	     onder.buf[i] = ' ';
        	
        	int value =0;
        	if (state == AVG){
              value = theHeartBeatController->getHbAvg();
              onder.buf[11] = 'A';
              onder.buf[12] = 'V';
              onder.buf[13] = 'G';
          }
        	else if (state == LOW){
            value = theHeartBeatController->getHbLow();
            onder.buf[11] = 'L';
            onder.buf[12] = 'O';
            onder.buf[13] = 'W';
          }
        	else if (state == HIGH){
            value = theHeartBeatController->getHbHigh();
            onder.buf[11] = 'H';
            onder.buf[12] = 'G';
            onder.buf[13] = 'H';
          }
        	if (value > 0){
        	  if (value < 100)
        	     onder.buf[2] = ' ';
        	     
            int_to_char(value, onder.buf);
            onder.buf[3] = ' '; //int_to_char conversie fix 
        	  theDisplayMonitor->display(onder);
        	  
          }
        }   	
        break;
  }

  buttonFlags.wait();

}

void MonitorController::buttonUp (Button* b){
  if (b == theSetButton){
    buttonFlags.set(0);
  } else if (b == theModeButton){
    buttonFlags.set(2);
  }
}
void MonitorController::buttonDown(Button* b){
  if (b == theSetButton){
    buttonFlags.set(1);
  } else if (b == theModeButton){
    buttonFlags.set(3);
  }
}


